package org.p2psimulator.entity.protocol.hibrid.eigentrust.eigencycles;

import java.util.List;

import org.p2psimulator.entity.Id;
import org.p2psimulator.entity.Message;
import org.p2psimulator.entity.Peer;
import org.p2psimulator.entity.protocol.messages.MessageTypes;
import org.p2psimulator.entity.protocol.messages.general.DownloadAnswerMessage;
import org.p2psimulator.entity.protocol.messages.general.DownloadRequestMessage;
import org.p2psimulator.engine.Environment;
import org.p2psimulator.entity.id.DoubleId;

public class EigenDownloadCycle extends EigenCycle {

	private Id serventId;
	private Id queryKey;
	private short avaliation = 1;

	public EigenDownloadCycle(Environment env, int transId) {
		super(env, transId);
	}

	@Override
	public String runRequestCycle(Peer p) {

		StringBuilder ret = new StringBuilder();


		DownloadRequestMessage down = new DownloadRequestMessage(
				p.getId(), serventId,
				currentTransactionId, p.getBehavior()
						.getBehaviorType());
		down.setTTL(1);
		down.setFileId(queryKey);

		env.getNetwork().sendMessage(down);

		ret.append(" [ DOWNLOAD CYCLE] [DOWNLOAD ENVIADO ] ");
		return ret.toString();
	}

	@Override
	public String runResponseCycle(Peer p) {
		StringBuilder ret = new StringBuilder();
		List<Message> downloadAnswares = p.getMessageStack().get(
				MessageTypes.DOWNLOAD_ANSWARE_MESSAGE.getName());

                avaliation = 0;
		if (downloadAnswares != null){

                    if (downloadAnswares.size() >0){
			DownloadAnswerMessage da = (DownloadAnswerMessage) downloadAnswares.get(0);

			if (da.isCorrupted()){
				avaliation = -1;
			}else{

                            if (!da.isNeutro()){
				avaliation = 1;
                                Id file = da.getFile();
                                List<Id> list = env.getFileSystem().getMatrix().get((DoubleId)file);
                                if (list != null){
                                    list.add(p.getId());
                                }
                            }else{
                                avaliation=-2;
                            }
			}
                    }else{
                        avaliation = 0;
                    }
                    ret.append("[ DOWNLOAD RESPONSE CYCLE ] [ARQUIVOS RECEBIDOS: " + downloadAnswares.size()+ "]");

		}else{
			avaliation = 0;
			ret.append("[ DOWNLOAD RESPONSE CYCLE ] [ARQUIVOS RECEBIDOS: 0 ]");
		}
                p.getProtocol().setTransactionResult(avaliation);
		return ret.toString();
	}

	@Override
	public String runSecondRequestCycle(Peer p) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String runSecondResponseCycle(Peer p) {
		// TODO Auto-generated method stub
		return null;
	}

	public Id getServentId() {
		return serventId;
	}

	public void setServentId(Id serventId) {
		this.serventId = serventId;
	}

	public int getAvaliation() {
		return avaliation;
	}

	public void setAvaliation(short avaliation) {
		this.avaliation = avaliation;
	}

	public Id getQueryKey() {
		return queryKey;
	}

	public void setQueryKey(Id queryKey) {
		this.queryKey = queryKey;
	}




}
